package cn.tedu.boot03.controller;

import cn.tedu.boot03.entity.Product;
import cn.tedu.boot03.utils.DBUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

@Controller
public class ProductController {
    @RequestMapping("/insert")
    @ResponseBody
    public String insert(Product product){
        System.out.println("product = " + product);
        //获取数据库连接
        try (Connection conn = DBUtils.getConn()){
            //准备执行插入的SQL语句
            String sql = "insert into product values(null,?,?,?)";
            //创建执行SQL语句的对象
            PreparedStatement ps = conn.prepareStatement(sql);
            //替换SQL语句中的三个?
            ps.setString(1,product.getTitle());
            ps.setInt(2,product.getPrice());
            ps.setInt(3,product.getNum());
            ps.executeUpdate();//执行插入数据的SQL语句
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return "添加完成!<a href='/'>返回首页</a>";
    }

    @RequestMapping("/select")
    @ResponseBody
    public String select(){
        //创建一个ArrayList集合对象 把查询到的多个商品对象保存
        ArrayList<Product> list = new ArrayList<>();

        //获取数据库连接
        try (Connection conn = DBUtils.getConn()){
            //准备执行查询的SQL语句
            String sql = "select id,title,price,num from product";
            //创建执行SQL语句的对象
            PreparedStatement ps = conn.prepareStatement(sql);
            //执行查询  , 查询到的结果在rs里面
            ResultSet rs = ps.executeQuery();
            //遍历结果集对象
            while(rs.next()){
                int id = rs.getInt(1);
                String title = rs.getString(2);
                int price = rs.getInt(3);
                int num = rs.getInt(4);
                //创建商品对象并把查询到的数据封装到对象中
                Product p = new Product();
                p.setId(id);
                p.setTitle(title);
                p.setPrice(price);
                p.setNum(num);
                //把对象装进list集合
                list.add(p);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        //把集合中的数据装进table表格中
        String html = "<table border='1'>";
        html+="<caption>商品列表</caption>";
        html+="<tr><th>id</th><th>标题</th><th>价格</th><th>库存</th><th>操作</th></tr>";
        //遍历集合 添加tr和td
        for (Product p:list) {
            html+="<tr>";
            html+="<td>"+p.getId()+"</td>";
            html+="<td>"+p.getTitle()+"</td>";
            html+="<td>"+p.getPrice()+"</td>";
            html+="<td>"+p.getNum()+"</td>";
            //添加删除超链接的一列, 请求地址为 /delete?id=xxx  ?是请求地址和参数的分隔符
            html+="<td><a href='/delete?id="+p.getId()+"'>删除</a></td>";
            html+="</tr>";
        }
        html+="</table>";
        return html;//把页面和数据一起返回给客户端
    }

    @RequestMapping("/delete")
    @ResponseBody
    public String delete(int id){
        System.out.println("id = " + id);
        //获取数据库连接
        try (Connection conn = DBUtils.getConn()){
            String sql = "delete from product where id=?";
            //创建执行SQL语句对象
            PreparedStatement ps = conn.prepareStatement(sql);
            //替换?
            ps.setInt(1,id);
            //执行插入SQL语句
            ps.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
//      http://localhost:8080/
        return "删除完成!<a href='/select'>返回列表页面</a>";
    }

    @RequestMapping("/update")
    @ResponseBody
    public String update(Product product){
        System.out.println("product = " + product);
        //获取数据库连接
        try (Connection conn = DBUtils.getConn()){
            String sql = "update product set title=?,price=?,num=? where id=?";
            //创建执行SQL语句对象
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1,product.getTitle());
            ps.setInt(2,product.getPrice());
            ps.setInt(3,product.getNum());
            ps.setInt(4,product.getId());
            ps.executeUpdate();//执行修改SQL语句
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return "修改完成!<a href='/select'>返回列表页面</a>";
    }
}
